home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / OLE_Files / VC++ / OLEShellDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  8.2 KB  |  321 lines

  1. // OLEShellDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "OLEShell.h"
  6. #include "OLEShellDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16.  
  17. class CAboutDlg : public CDialog
  18. {
  19. public:
  20.     CAboutDlg();
  21.  
  22. // Dialog Data
  23.     //{{AFX_DATA(CAboutDlg)
  24.     enum { IDD = IDD_ABOUTBOX };
  25.     //}}AFX_DATA
  26.  
  27.     // ClassWizard generated virtual function overrides
  28.     //{{AFX_VIRTUAL(CAboutDlg)
  29.     protected:
  30.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  31.     //}}AFX_VIRTUAL
  32.  
  33. // Implementation
  34. protected:
  35.     //{{AFX_MSG(CAboutDlg)
  36.     //}}AFX_MSG
  37.     DECLARE_MESSAGE_MAP()
  38. };
  39.  
  40. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CAboutDlg)
  43.     //}}AFX_DATA_INIT
  44. }
  45.  
  46. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  47. {
  48.     CDialog::DoDataExchange(pDX);
  49.     //{{AFX_DATA_MAP(CAboutDlg)
  50.     //}}AFX_DATA_MAP
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CAboutDlg)
  55.         // No message handlers
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // COLEShellDlg dialog
  61.  
  62. COLEShellDlg::COLEShellDlg(CWnd* pParent /*=NULL*/)
  63.     : CDialog(COLEShellDlg::IDD, pParent)
  64. {
  65.     //{{AFX_DATA_INIT(COLEShellDlg)
  66.     m_fY = 0.0f;
  67.     m_fX = 0.0f;
  68.     m_sOutput = _T("");
  69.     //}}AFX_DATA_INIT
  70.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  71.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  72. }
  73.  
  74. void COLEShellDlg::DoDataExchange(CDataExchange* pDX)
  75. {
  76.     CDialog::DoDataExchange(pDX);
  77.     //{{AFX_DATA_MAP(COLEShellDlg)
  78.     DDX_Text(pDX, IDC_EDIT2, m_fY);
  79.     DDX_Text(pDX, IDC_EDIT1, m_fX);
  80.     DDX_Text(pDX, IDC_output, m_sOutput);
  81.     //}}AFX_DATA_MAP
  82. }
  83.  
  84. BEGIN_MESSAGE_MAP(COLEShellDlg, CDialog)
  85.     //{{AFX_MSG_MAP(COLEShellDlg)
  86.     ON_WM_SYSCOMMAND()
  87.     ON_WM_PAINT()
  88.     ON_WM_QUERYDRAGICON()
  89.     ON_BN_CLICKED(IDC_BUTTON1, OnInitOLE)
  90.     ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
  91.     ON_EN_KILLFOCUS(IDC_EDIT2, OnKillfocusEdit2)
  92.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  93.     ON_WM_CLOSE()
  94.     //}}AFX_MSG_MAP
  95. END_MESSAGE_MAP()
  96.  
  97. /////////////////////////////////////////////////////////////////////////////
  98. // COLEShellDlg message handlers
  99.  
  100. BOOL COLEShellDlg::OnInitDialog()
  101. {
  102.     CDialog::OnInitDialog();
  103.  
  104.     // Add "About..." menu item to system menu.
  105.  
  106.     // IDM_ABOUTBOX must be in the system command range.
  107.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  108.     ASSERT(IDM_ABOUTBOX < 0xF000);
  109.  
  110.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  111.     CString strAboutMenu;
  112.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  113.     if (!strAboutMenu.IsEmpty())
  114.     {
  115.         pSysMenu->AppendMenu(MF_SEPARATOR);
  116.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  117.     }
  118.  
  119.     // Set the icon for this dialog.  The framework does this automatically
  120.     //  when the application's main window is not a dialog
  121.     SetIcon(m_hIcon, TRUE);            // Set big icon
  122.     SetIcon(m_hIcon, FALSE);        // Set small icon
  123.     
  124.     // TODO: Add extra initialization here
  125.     m_bBreadboardOpen = FALSE;
  126.     return TRUE;  // return TRUE  unless you set the focus to a control
  127. }
  128.  
  129. void COLEShellDlg::OnSysCommand(UINT nID, LPARAM lParam)
  130. {
  131.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  132.     {
  133.         CAboutDlg dlgAbout;
  134.         dlgAbout.DoModal();
  135.     }
  136.     else
  137.     {
  138.         CDialog::OnSysCommand(nID, lParam);
  139.     }
  140. }
  141.  
  142. // If you add a minimize button to your dialog, you will need the code below
  143. //  to draw the icon.  For MFC applications using the document/view model,
  144. //  this is automatically done for you by the framework.
  145.  
  146. void COLEShellDlg::OnPaint() 
  147. {
  148.     if (IsIconic())
  149.     {
  150.         CPaintDC dc(this); // device context for painting
  151.  
  152.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  153.  
  154.         // Center icon in client rectangle
  155.         int cxIcon = GetSystemMetrics(SM_CXICON);
  156.         int cyIcon = GetSystemMetrics(SM_CYICON);
  157.         CRect rect;
  158.         GetClientRect(&rect);
  159.         int x = (rect.Width() - cxIcon + 1) / 2;
  160.         int y = (rect.Height() - cyIcon + 1) / 2;
  161.  
  162.         // Draw the icon
  163.         dc.DrawIcon(x, y, m_hIcon);
  164.     }
  165.     else
  166.     {
  167.         CDialog::OnPaint();
  168.     }
  169. }
  170.  
  171. // The system calls this to obtain the cursor to display while the user drags
  172. //  the minimized window.
  173. HCURSOR COLEShellDlg::OnQueryDragIcon()
  174. {
  175.     return (HCURSOR) m_hIcon;
  176. }
  177.  
  178. void COLEShellDlg::OnInitOLE() 
  179. {
  180.     COLEShellApp *myApp = (COLEShellApp *)AfxGetApp();
  181.     if (!m_nsApp.CreateDispatch(_T("NeuroSolutions.Application"))) {
  182.         AfxMessageBox("Cannot open NeuroSolutions application");
  183.         return;
  184.     }
  185.     m_nsApp.restore();
  186.     CString relativePath = m_nsApp.pathFromNS("OLE\\Breadboard\\MLPXor.nsb");
  187.     m_nsObject.AttachDispatch(m_nsApp.open(relativePath));
  188.     m_nsObject.maximize();
  189.     m_bBreadboardOpen = TRUE;
  190. }
  191.  
  192. void COLEShellDlg::perform(LPCSTR aString)
  193. {
  194.     m_nsObject.send(aString);     
  195. }
  196.  
  197. int COLEShellDlg::performReturningInt(LPCSTR aString)
  198. {
  199.     VARIANT var = m_nsObject.send(aString);
  200.     ASSERT(V_VT(&var) == VT_I4);
  201.     return var.lVal;
  202. }
  203.  
  204. void COLEShellDlg::performSendingFloatArray(float *data, int rows, int cols, LPCSTR aString)
  205. {
  206.     VARIANT vaResult;
  207.     VariantInit(&vaResult);
  208.     SAFEARRAY FAR* psa;
  209.     SAFEARRAYBOUND sabound[2];
  210.     sabound[0].cElements = rows;
  211.     sabound[0].lLbound = 0;
  212.     sabound[1].cElements = cols;
  213.     sabound[1].lLbound = 0;
  214.     psa = SafeArrayCreate(VT_VARIANT, 2, sabound);
  215.     long n[2];
  216.     for (n[0]=0; n[0]<rows; n[0]++)
  217.         for (n[1]=0; n[1]<cols; n[1]++) {
  218.             VARIANT vItem;
  219.             VariantInit(&vItem);
  220.             vItem.vt = VT_R4;
  221.             vItem.fltVal = *(data + (n[1]*rows) + n[0]);
  222.             if (SafeArrayPutElement(psa, n, &vItem) != S_OK)
  223.                 AfxMessageBox("Error: could not write data to variant");
  224.         }
  225.     V_VT(&vaResult) = VT_ARRAY | VT_VARIANT;
  226.     V_ARRAY(&vaResult) = psa;
  227.     m_nsObject.sendDataToEngine(&vaResult, aString);
  228. }
  229.  
  230. float COLEShellDlg::performReturningFloat(LPCSTR aString)
  231. {
  232.     VARIANT var = m_nsObject.send(aString);
  233.     ASSERT(V_VT(&var) == VT_R4);
  234.     return var.fltVal;
  235. }
  236.  
  237. float *COLEShellDlg::performReturningFloatArray(LPCSTR aString, int *rows, int *cols)
  238. {
  239.     VARIANT var = m_nsObject.send(aString);
  240.     ASSERT(V_VT(&var) == (VT_ARRAY | VT_R4));
  241.     LPSAFEARRAY psa = V_ARRAY(&var);
  242.     int iDims = SafeArrayGetDim(psa);
  243.     long lRowLBound, lRowUBound, lColLBound, lColUBound;
  244.     SafeArrayGetLBound(psa,1,&lColLBound);
  245.     SafeArrayGetUBound(psa,1,&lColUBound);
  246.     SafeArrayGetLBound(psa,2,&lRowLBound);
  247.     SafeArrayGetUBound(psa,2,&lRowUBound);
  248.     *rows = (int)lRowUBound-(int)lRowLBound+1, *cols = (int)lColUBound-(int)lColLBound+1;
  249.     float *aBuffer = (float *)malloc(*rows * *cols * sizeof(float));
  250.     long n[2], i;
  251.     i = 0;
  252.     for (n[1]=lRowLBound; n[1]<=lRowUBound; n[1]++) {
  253.         for (n[0]=lColLBound; n[0]<=lColUBound; n[0]++) {
  254.             float fItem;
  255.             if (SafeArrayGetElement(psa, n, &fItem) == S_OK)
  256.                 aBuffer[i++] = fItem;
  257.             else
  258.                 AfxMessageBox("Error: could not read variant array.");
  259.         }
  260.     }
  261.     return aBuffer;
  262. }
  263.  
  264. BOOL COLEShellDlg::performReturningBOOL(LPCSTR aString)
  265. {
  266.     VARIANT var = m_nsObject.send(aString);
  267.     ASSERT((V_VT(&var) == VT_I4) || (V_VT(&var) == VT_I2));
  268.     return (BOOL)((V_VT(&var) == VT_I4) ? var.lVal : var.iVal);
  269. }
  270.  
  271. CString COLEShellDlg::performReturningString(LPCSTR aString)
  272. {
  273.     VARIANT var = m_nsObject.send(aString);
  274.     ASSERT(V_VT(&var) == VT_BSTR);
  275.     CString tempString = var.bstrVal;
  276.     SysFreeString(var.bstrVal);
  277.     return tempString;
  278. }
  279.  
  280. void COLEShellDlg::OnKillfocusEdit1() 
  281. {
  282.     UpdateData(TRUE);
  283. }
  284.  
  285. void COLEShellDlg::OnKillfocusEdit2() 
  286. {
  287.     UpdateData(TRUE);
  288. }
  289.  
  290. void COLEShellDlg::OnButton2() 
  291. {
  292.     if (!m_bBreadboardOpen)
  293.         AfxMessageBox("You must open the breadboard first.");
  294.     else {
  295.         int inputRows = 2;
  296.         int inputCols = 1;
  297.         float *data = (float *)malloc(inputRows * inputCols * sizeof(float));
  298.         *data = m_fX;
  299.         *(data +1) = m_fY;
  300.         performSendingFloatArray(data, inputRows, inputCols, "oLEInput");
  301.         perform("control.stepExemplar()");
  302.         int outputRows = 1;
  303.         int outputCols = 1;
  304.         float *results = performReturningFloatArray("matrixViewer. getProbeData()", &outputRows, &outputCols);
  305.         if (results) {
  306.             m_sOutput.Format("%f",*results);
  307.             free(results);
  308.         }
  309.     }
  310.     UpdateData(FALSE);
  311. }
  312.  
  313. void COLEShellDlg::OnClose() 
  314. {
  315.     m_nsObject.DetachDispatch();
  316.     m_nsApp.DetachDispatch();
  317.     m_nsObject.ReleaseDispatch();
  318.     m_nsApp.ReleaseDispatch();
  319.     CDialog::OnClose();
  320. }
  321.